Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

this.timeout() inside describe() doesn't work for me #779

Closed
alexlatchford opened this issue Mar 19, 2013 · 17 comments
Closed

this.timeout() inside describe() doesn't work for me #779

alexlatchford opened this issue Mar 19, 2013 · 17 comments

Comments

@alexlatchford
Copy link

This below JS snippet is from the mocha.js website docs, "Suite specific timeouts":

describe('a suite of tests', function(){
  this.timeout(500);

  it('should take less than 500ms', function(done){
    setTimeout(done, 300);
  })

  it('should take less than 500ms as well', function(done){
    setTimeout(done, 200);
  })
})

This code example is a bit confusing, when I put the "this.timeout()" call like this I get:

C:\Dev\GitHub\alexlatchford\adfuser\src\test\api\v1\target_groups.js:274
                this.timeout(0); // Extend the timeout for this suite because we're insertin
                     ^
TypeError: Object #<Object> has no method 'timeout'
    at C:\Dev\GitHub\alexlatchford\adfuser\src\test\api\v1\target_groups.js:274:8
    at module.exports.suite.on.context.describe.context.context (C:\Users\alatchford\AppData\Roaming\npm\node_modules\mocha\lib\interfaces\bdd.js:72:7)
    at C:\Dev\GitHub\alexlatchford\adfuser\src\test\api\v1\target_groups.js:272:2
    at module.exports.suite.on.context.describe.context.context (C:\Users\alatchford\AppData\Roaming\npm\node_modules\mocha\lib\interfaces\bdd.js:72:7)
    at Object.<anonymous> (C:\Dev\GitHub\alexlatchford\adfuser\src\test\api\v1\target_groups.js:15:1)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:362:17)
    at require (module.js:378:17)
    at Mocha.loadFiles (C:\Users\alatchford\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js:137:27)
    at Array.forEach (native)
    at Mocha.loadFiles (C:\Users\alatchford\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js:134:14)
    at Mocha.run (C:\Users\alatchford\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js:278:31)
    at Object.<anonymous> (C:\Users\alatchford\AppData\Roaming\npm\node_modules\mocha\bin\_mocha:324:7)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain (module.js:492:10)
    at process.startup.processNextTick.process._tickCallback (node.js:245:9)

It works if I put it inside my before() for that group of tests, which incidentally was what I intended anyway, but I think either the docs need clarifying or the error message investigating :)

Cheers,
Alex

@rweng
Copy link

rweng commented Oct 22, 2013

+1

@Thomasdezeeuw
Copy link

I have the same problem with the "before all" hook. It graps the timeout that I specified globally in stead of the one in the function with this.timeout(2000);

@ghost
Copy link

ghost commented May 14, 2014

Hello, are you there? Can someone fix this? I found out two places in code where this is problem, probably - timeout methods calling other timeout methods, but not doing apply(this, arguments) but passing the first one by hand, thus killing the if length === 0 readTheValue semantics.

As the system is pretty complex, you know better if adding apply there is enough, if yes, the fix should be easy.

@dmakhno
Copy link

dmakhno commented Jul 24, 2014

+1

@travisjeffery
Copy link
Contributor

i think i recently fixed this. your code worked for me too:

describe('something', function(){
  this.timeout(500);

  it('should take less than 500', function(done){
    setTimeout(done, 300);
  });

  it('should take less than 500 too', function(done){
    setTimeout(done, 200);
  });
})
🍕  mocha


  something
    ✓ should take less than 500 (301ms)
    ✓ should take less than 500 too (201ms)


  2 passing (510ms)

@dmakhno
Copy link

dmakhno commented Jul 24, 2014

@travisjeffery,

I voted for this

I have the same problem with the "before all" hook.

all

before(function(){
  this.timeout(500);
})

describe('something', function(){
  it('should take less than 500', function(done){
    setTimeout(done, 300);
  });

  it('should take less than 500 too', function(done){
    setTimeout(done, 200);
  });
})
beforeEach(function(){
  this.timeout(500);
})

describe('something', function(){
  it('should take less than 500', function(done){
    setTimeout(done, 300);
  });

  it('should take less than 500 too', function(done){
    setTimeout(done, 200);
  });
})
describe('something', function(){  
  beforeEach(function(){
    this.timeout(500);
  })

  it('should take less than 500', function(done){
    setTimeout(done, 300);
  });

  it('should take less than 500 too', function(done){
    setTimeout(done, 200);
  });
})

returns this

mocha --version
   1.20.1

mocha --timeout 100

  something
    1) should take less than 500
    2) should take less than 500 too


  0 passing (215ms)
  2 failing

  1) something should take less than 500:
     Error: timeout of 100ms exceeded

  2) something should take less than 500 too:
     Error: timeout of 100ms exceeded

my expectations that 500 should be applied for each test

Should I create separate issue?

@travisjeffery
Copy link
Contributor

ok i'm gonna close this then. hopefully you can look into making a pr rather than an issue for the befores.

@ghost
Copy link

ghost commented Jul 24, 2014

Please reopen, this.timeout() still fails, see https://github.com/visionmedia/mocha/blob/master/lib/context.js#L39 calling directly with ms argument thus making length of arguments always 1 in versus https://github.com/visionmedia/mocha/blob/master/lib/runnable.js#L66 which can never be true and return the value.

@travisjeffery
Copy link
Contributor

the case where that would fail is if you're getting the value of the timeout not setting it

@ghost
Copy link

ghost commented Jul 25, 2014

Yes, that's what I had a problem with in the first place.

"Dr. Travis Jeffery" notifications@gh.neting.ccnapísal/a:

thecasewherethatwouldfailisifyou'regettingthevalueofthetimeoutnotsettingitReplytothisemaildirectlyorviewitonGitHub.

@travisjeffery
Copy link
Contributor

@Herby fixed in #1282

@ghost
Copy link

ghost commented Jul 25, 2014

@travisjeffery thanks

@ghost
Copy link

ghost commented Oct 19, 2016

fyi, still seeing this in 3.1.2

describe('something', () => {
  this.timeout(5000);
  it('should work', done => {
    setTimeout(done, 2000);
  });
});

@Macil
Copy link

Macil commented Oct 19, 2016

@JacobRodriguezSSI By using an arrow function, you're not using the this value passed to describe's callback. You should use a normal function instead.

@ghost
Copy link

ghost commented Oct 19, 2016

@agentme: You're right, sorry. In my haste I didn't see the other issue that pointed out this fact.

@ghost
Copy link

ghost commented Jun 29, 2017

For posterity, you can use arrow functions and set the timeout like this:

it('some test', (done) => {
    // ...
}).timeout(5000);

@selvakumar1994
Copy link

selvakumar1994 commented Aug 29, 2018

@travisjeffery i used the below script but i got the same Timeout exceed error.

Myscript :

describe("getBillingDetail", async function (){
this.timeout(55000);
it.only("check given valid Jobname",async function (done){
this.timeout(55000);
var result = await url.getBillingDetail('12254785565647858');
console.log(result);
assert.equal(result,true);
});
});

Error: Timeout of 55000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants